package com.tebiecloud.yunst.server.service.impl;

import com.tebiecloud.yunst.client.model.entity.YunRefund;
import com.tebiecloud.yunst.server.mapper.YunRefundMapper;
import com.tebiecloud.yunst.server.service.YunRefundService;
import com.tebiecloud.common.mybatis.base.service.impl.BaseServiceImpl;
import com.tebiecloud.yunst.server.yunstservice.YunStService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;

/**
 * 云商通退款表 服务实现类
 * @author tebie
 * @date 2019-09-07
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class YunRefundServiceImpl extends BaseServiceImpl<YunRefundMapper, YunRefund> implements YunRefundService {

    @Value("${tebiecloud.yunst.backUrl}")
    private String backUrl;


    @Autowired
    YunStService yunStService;

    @Autowired
    YunRefundMapper yunRefundMapper;

    @Override
    public HashMap<String, Object> refund(String bizUserId, Long orderId,String oriBizOrderNo, Long amount) {
        HashMap<String, Object> resultMap = new HashMap<String, Object>();
        String realBackUrl = backUrl + "/order/ordercallback/refundCallBack?orderId="+orderId;
        HashMap<String, String> map =  yunStService.refund(bizUserId,oriBizOrderNo,amount,backUrl);
        if(map.get("status").equals("OK") && !map.get("payStatus").equals("fail")){
            YunRefund yunRefund = new YunRefund();
            yunRefund.setBizOrderNo(map.get("bizOrderNo"));
            yunRefund.setOrderId(orderId);
            yunRefund.setAmount(amount.intValue());
            yunRefund.setOrgBizOrderNo(oriBizOrderNo);
            yunRefund.setBizUserId(bizUserId);
            yunRefund.setPayStatus(1);
            yunRefundMapper.insert(yunRefund);
            resultMap.put("status","OK");
            resultMap.put("yunRefund",yunRefund);
        }else{
            resultMap.put("status","error");
            resultMap.put("message",map.get("message")==null?map.get("payFailMessage"):map.get("message"));
        }
        return resultMap;
    }
}
